package com.wwwc.index.web.servlet;

import java.io.*;
import java.awt.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequestWrapper;
import java.net.*;

import com.wwwc.util.web.*;
//import com.wwwc.index.web.database.*;
import com.wwwc.index.web.ejb.database.*;

public class AccountManager {
    private EJBDatabaseQuery ejbdb;
    String query = null;

    public StringBuffer getManager (String sub_dir, String user_name)
    {
	StringBuffer sbf = new StringBuffer("");
	sbf.append("\n<TABLE cellSpacing='0' cellPadding='0' border='0' width='100%' style='border-bottom:1px solid'>");
	sbf.append("\n<TR>");
	sbf.append("\n<TD align=center style='color:#6600CC; font-size:10pt' bgcolor='#669999'>");
	sbf.append("<B>"+user_name.toUpperCase()+"'s/"+sub_dir+"</B>");
	sbf.append("</TD>");
	sbf.append("\n<TD align=center style='color:#6600CC; font-size:10pt' bgcolor='#999966'>");
	sbf.append("Manager:&nbsp;"+user_name+"</TD>");
	sbf.append("\n</TR>");
	sbf.append("\n</TABLE>");
	return sbf;
    }

    public StringBuffer getLinkBar(String call_path, String mid, String view_profile, String change_pass, String update_profile) {
	StringBuffer sbf = new StringBuffer();

	sbf.append("\n<TABLE cellSpacing='5' cellPadding='0' border='0'>");
	sbf.append("\n<TR>");
	sbf.append(MyHtml.makeLinkCell("View Profile", call_path+"?mid="+mid+"&aid="+view_profile));	
	sbf.append(MyHtml.makeLinkCell("Change Password", call_path+"?mid="+mid+"&aid="+change_pass));
	sbf.append(MyHtml.makeLinkCell("Update Profile", call_path+"?mid="+mid+"&aid="+update_profile));
	sbf.append("\n</TR>");
	sbf.append("\n</TABLE>");
	return sbf;
    }

    public StringBuffer getUserProfile (String user_name, int user_level) {
	StringBuffer sbf = new StringBuffer("");
	try {
	    if(user_name == null || user_name.equals("guest")) {
		return sbf;
	    }
	    
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }

	    query = "SELECT * FROM USER_INFO WHERE USERNAME='"+user_name+"';";
	    ArrayList a = ejbdb.selectSingleRecord(query);
	    String temp = null;
	    sbf.append("<TABLE border='0' width='100%'>");
	    sbf.append("<TR><TD style='font-size: 14pt' align='center'>"+user_name+"'s profile</TD></TR>");
	    
	    sbf.append("<TR><TD></TD></TR>");
	    sbf.append("</TABLE>");
	    sbf.append("<TABLE border='0' width='100%'>");
	    sbf.append("<TR><TD align='right' style='font-size: 12pt'>User Name:</TD>");
	    sbf.append("<TD>&nbsp;</TD>");
	    sbf.append("<TD align='left' style='font-size: 10pt'>"+a.get(0)+"</TD>");
	    sbf.append("</TR>");

	    sbf.append("<TR>");
	    sbf.append("<TD align='right' style='font-size: 12pt'>Current Email:</TD>");
	    sbf.append("<TD>&nbsp;</TD>");
	    sbf.append("<TD align='left'  style='font-size: 10pt'>"+a.get(1)+"</TD>");
	    sbf.append("</TR>");

	    sbf.append("<TR>");
	    sbf.append("<TD align='right' style='font-size: 12pt'>First Name:</TD>");
	    sbf.append("<TD>&nbsp;</TD>");
	    sbf.append("<TD align='left' style='font-size: 10pt'>"+a.get(2)+"</TD>");
	    sbf.append("</TR>");

	    sbf.append("<TR>");
	    sbf.append("<TD align='right' style='font-size: 12pt'>Last Name:</TD>");
	    sbf.append("<TD>&nbsp;</TD>");
	    sbf.append("<TD align='left' style='font-size: 10pt'>"+a.get(3)+"</TD>");
	    sbf.append("</TR>");

	    sbf.append("<TR>");
	    sbf.append("<TD align='right' style='font-size: 12pt'>Birthday:</TD>"); 
	    sbf.append("<TD>&nbsp;</TD>");
	    sbf.append("<TD style='font-size: 10pt'>");
	    sbf.append(a.get(4));
	    sbf.append("</TD>");
	    sbf.append("</TR>");

	    sbf.append("<TR>");
	    sbf.append("<TD align='right' style='font-size: 12pt'>Gender:</TD>"); 
	    sbf.append("<TD>&nbsp;</TD>");
	    sbf.append("<TD style='font-size: 10pt'>");

	    temp = (String)a.get(6);
	    sbf.append(temp);
	    sbf.append("</TD>");
	    sbf.append("</TR>");

	    sbf.append("<TR>");
	    sbf.append("<TD align='right' style='font-size: 12pt'>Education:</TD>");
	    sbf.append("<TD>&nbsp;</TD>");
 	    sbf.append("<TD style='font-size: 10pt'>");
	    temp = (String)a.get(7);
	    if(temp.equals("0")) {
		sbf.append("High-School");
	    }
	    else if(temp.equals("1")) {
		sbf.append("College(2 Years)");
	    }
	    else if(temp.equals("2")) {
		sbf.append("Bachelor");
	    }
	    else if(temp.equals("3")) {
		sbf.append("Master");
	    }
	    else if(temp.equals("4")) {
		sbf.append("Ph.D/Dr.");
	    }
	    sbf.append("</TD>");
	    sbf.append("</TR>");

	    sbf.append("<TR>");
	    sbf.append("<TD align=right style='font-size: 12pt'>Zip/postal code:</TD>"); 
	    sbf.append("<TD>&nbsp;</TD>");
	    sbf.append("<TD style='font-size: 10pt'>");
	    sbf.append(a.get(8));
	    sbf.append("</TD>");
	    sbf.append("</TR>");
	    sbf.append("<TR>");
	    sbf.append("<TD align='right' style='font-size: 12pt'>Membership Level:</TD>"); 
	    sbf.append("<TD>&nbsp;</TD>");
	    sbf.append("<TD style='font-size: 10pt'>");
	    sbf.append(user_level);
	    sbf.append("</TD>");

	    sbf.append("</TR>");

	    sbf.append("</TABLE>");
	}
	catch(Exception e) {
	    System.out.println("Error:AccountManager:["+e+"]");
	}
	return sbf;
    }

    public StringBuffer changePassword (HttpServletRequest request, String user_name) {
	//String call_path = request.getServletPath();
	String call_path     = request.getContextPath()+request.getServletPath();

	String mid         = (String)request.getAttribute("mid");
	String aid         = request.getParameter("aid");
	String submit_id   = request.getParameter("submit_id");
	StringBuffer sbf   = new StringBuffer("");
	StringBuffer error = null;
	HttpSession session  = request.getSession(true);

	if(submit_id != null) {
	    session  = request.getSession(true);
	    if(!submit_id.equals(session.getAttribute("changePassSubmitId"))) {
		return sbf;
	    }
	    if(checkChangePassword(request, user_name)) {
		sbf.append("<BR><BR><H3>Password has been changed</H3>");
		session.setAttribute("changePassSubmitId",null);
		return sbf;
	    }
	    else {
		error = new StringBuffer("Change Password Error.");
	    }
	}

	submit_id = ""+System.currentTimeMillis();
	session.setAttribute("changePassSubmitId",submit_id);

	sbf.append("<CENTER>");
	sbf.append("<FORM name='passform' method='post' action='"+call_path+"'>");
	sbf.append("<TABLE border='0' width='100%'>");
	sbf.append("<TR><TD style='font-size: 14pt' align='center'>Change Password</TD></TR>");

	if(error != null) {
	    sbf.append("<TR><TD style='font-size: 12pt' align='center'><img src='/images/star.gif' border='0'>");
	    sbf.append("<I>Change Password Error!</I></TD></TR>");
	}

	sbf.append("<TR><TD></TD></TR>");
	sbf.append("</TABLE>");

	sbf.append("<TABLE border='0' width='100%'>");
	sbf.append("<TR><TD align='right' style='font-size: 12pt'>Current Password:</TD>");
	sbf.append("<TD><INPUT maxLength=20 name=pass value='' type=password size=20></TD>");
	sbf.append("</TR>");

	sbf.append("<TR>");
	sbf.append("<TD align='right' style='font-size: 12pt'>New Password:</TD>");
	sbf.append("<TD><INPUT maxLength=20 name=pass1 value='' type=password size=20></TD>");
	sbf.append("</TR>");

	sbf.append("<TR>");
	sbf.append("<TD align='right' style='font-size: 12pt'>Re-type New Password:</TD>");
	sbf.append("<TD><INPUT maxLength=20 name=pass2 value='' type=password size=20></TD>");
	sbf.append("</TR>");
	sbf.append("</TABLE>");
	sbf.append("<BR>");
	sbf.append("<TABLE border=0 width='100%'>");
	sbf.append("<TR>");
	sbf.append("<TD align=center>");
	sbf.append("<input type='submit' value='Submit' name='B1' style='font-size: 8pt'>");
	sbf.append("<input type='reset' value='Reset' name='B2' style='font-size: 8pt'>");
	sbf.append("</TD>");
	sbf.append("</TR>");
	sbf.append("</TABLE>");

	sbf.append("<input type=hidden name='submit_id' value='"+submit_id+"'>");
	sbf.append("<input type=hidden name='mid' value='"+mid+"'>");
	sbf.append("<input type=hidden name='aid' value='"+aid+"'>");
	sbf.append("</FORM>");
	sbf.append("</CENTER>");
	return sbf;
    }

    private boolean checkChangePassword (HttpServletRequest request, String user_name) {
	String pass  = request.getParameter("pass");
	String pass1 = request.getParameter("pass1");
	String pass2 = request.getParameter("pass2");

	if(pass == null || pass1 == null || pass2 == null || !pass1.equals(pass2) || 
	   pass.length()<1 || pass1.length()<4 || pass2.length()<4) {
	    return false;
	}

	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }

	    String query = "SELECT PASSWD FROM USERS WHERE USERNAME='"+user_name+"';";
	    String temp  = ejbdb.selectSingleFieldValue(query, "PASSWD");
		
	    if(temp == null || temp.length() <1 || !temp.equals(pass)) {
		return false;
	    }
	    else {
		if(pass1.indexOf("'") != -1) {
		    pass1 = pass1.replaceAll("'","''");
		}
		    
		query = "UPDATE USERS SET PASSWD='"+pass1+"' WHERE USERNAME='"+user_name+"';";
		if(ejbdb.update(query) !=1) {
		    return false;
		}
	    }
	}
	catch (IOException e) {
	    return false;
	}

	return true;
    }

    public StringBuffer updateUserProfile (HttpServletRequest request, String mid, String submit_id, String user_name)
    {
	String fname     = request.getParameter("user_fname");
	String lname     = request.getParameter("user_lname");
	String user_edu  = request.getParameter("user_edu");
	String zipcode   = request.getParameter("user_zip");
	String call_path = request.getContextPath()+ request.getServletPath();
	String aid       = request.getParameter("aid");

	StringBuffer message = new StringBuffer("");

	if(fname == null || lname == null || zipcode == null || user_edu == null) {
	    fname    = "";
	    lname    = "";
	    zipcode  = "";
	    user_edu = "-1";
	}
	else {
	    String format1 = "ABCDEFGHIJKLMNOPQRSTUVWSYZabcdefghijklmnopqrstuvwxyz-'";
	    String format2 =format1+"_0123456789";

	    MyUserInputCheck ck = new MyUserInputCheck();

	    if(fname.length()<1) {
		message.append("Please enter your First name!");
	    }
	    else if(lname.length()<1) {
		message.append("Please enter your Last name!");
	    }
	    else if(!ck.checkFormat("First Name", fname, format1)){
		message.append(ck.getMessage());
	    }
	    else if(!ck.checkFormat("Last Name", lname, format1)) {
		message.append(ck.getMessage());
	    }
	    else if(user_edu.equals("-1")) {
		message.append("Please select your education level!");
	    }
	    else if(!ck.checkZipCode(zipcode)) {
		message.append(ck.getMessage());
	    }

	    if(message.length()<1) {
		try {
		    if (ejbdb == null) {
			ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
		    }

		    String query = null;

		    query = "UPDATE USER_INFO SET FIRSTNAME='"+fname+"', LASTNAME='"+lname+"', EDUCATION='"+
			user_edu+"', ZIPCODE='"+zipcode+"' WHERE USERNAME='"+user_name+"';";
		    if(ejbdb.update(query) !=1) {
			message.append("Update Error!");
		    }
		}
		catch (IOException e) {
		    message.append("Update Error!");
		}

		if(message.length()<1) {
		    return new StringBuffer("<BR><BR><BR><H3>Your profile has been updated!</H3>");
		}
	    }
	}

	StringBuffer sbf = new StringBuffer();
	sbf.append("<CENTER>");
	sbf.append("<FORM name=userform method=post action='"+call_path+"'>");
	sbf.append("<TABLE border=0 width='100%'>");
	sbf.append("<TR><TD style='font-size: 16pt' align='center'>All fields are required.</TD></TR>");

	if(message != null && message.length()>1) { 
	    sbf.append("<TR><TD style='font-size: 10pt' align='center'>");
	    sbf.append("<img src=/images/star.gif border=0><I>"+message+"</I>");
	    sbf.append("</TD></TR>");
	}

	sbf.append("<TR><TD></TD></TR>");
	sbf.append("</TABLE>");

	sbf.append("<TABLE border='0' width='100%'>");

	sbf.append("<TR>");
	sbf.append("<TD align='right' width='50%'>First Name:</TD>");
	sbf.append("<TD align='left' width='50%'><INPUT maxLength=50 name=user_fname value='"+fname+"' size=10></TD>");
	sbf.append("</TR>");
	sbf.append("<TR>");
	sbf.append("<TD align='right' width='50%'>Last Name:</TD>");
	sbf.append("<TD align='left' width='50%'><INPUT maxLength=50 name=user_lname value='"+lname+"' size=10></TD>");
	sbf.append("</TR>");

	sbf.append("<TR>");
	sbf.append("<TD align='right' width='50%'>Education:</TD>"); 
	sbf.append("<TD align='left' width='50%'>");
	sbf.append("<SELECT name='user_edu'>");
	sbf.append("<OPTION value='-1'>----------------</OPTION>");
	sbf.append(MyHtml.getEducationOption(user_edu));
	sbf.append("</SELECT>");
	sbf.append("</TD>");
	sbf.append("</TR>");

	sbf.append("<TR>");
	sbf.append("<TD align='right' width='50%'>Zip/Postal code:</TD>"); 
	sbf.append("<TD align='left' width='50%'><input type='text' size='10' maxLength='10' name='user_zip' value='"+zipcode+"'>");
	sbf.append("</TD>");
	sbf.append("</TR>");

	sbf.append("</TABLE>");
	sbf.append("<BR>");
	sbf.append("<TABLE border=0 width='100%'>");
	sbf.append("<TR>");
	sbf.append("<TD align=center>");
	sbf.append("<input type='submit' value='Submit' name='B1' style='font-size: 8pt'>");
	sbf.append("<input type='reset' value='Reset' name='B2' style='font-size: 8pt'>");
	sbf.append("</TD>");
	sbf.append("</TR>");
	sbf.append("</TABLE>");

	sbf.append("<input type=hidden name=submit_id value='"+submit_id+"'>");
	sbf.append("<input type=hidden name=mid value='"+mid+"'>");
	sbf.append("<input type=hidden name=aid value='"+aid+"'>");
	sbf.append("</FORM>");

	sbf.append("</CENTER>");
	return sbf;
    }
}






